<?php
class Attention
{
    private $_id;
    private $_uid;
    private $_table;
    private $_db;
    
    public function __construct($id=0) {
        global $db;
        $this->_id=$id;
        $this->_db=&$db;
        $this->_table = $this->_db->pre."attention";
    }
    private function _check($info)
    {
        if(!$info['uid'])
        {
            return FALSE;
        }
        if(!$info['aid'])
        {
            return FALSE;
        }
        //检测重复
        if($this->_id)
        {
            $sql = "SELECT id FROM $this->_table WHERE id='$this->_id'";
            $one = $db->getOne($sql);
            if($one)
            {
                return FALSE;
            }  else {
                return TRUE;
            }
        }else{
            return TRUE;
        }
    }
    private function _set($info) {
        if(!$this->_id){
            $info['addtime'] = time();
        }
        $this->_uid = $info['uid'] = intval($info['uid'])?intval($info['uid']):$_userid;
        $info['aid'] = intval($info['aid']);
        return $info;
    }
    public function add($info)
    {
        $info  = $this->_set($info);
        if($this->_check($info)){
            $sql = Sql::insert($this->_table, $info);
            return $this->_db->query($sql);
        }
    }
    public function delete()
    {
        return $this->_db->query(Sql::delete($this->_table, "id='$this->_id'"));
    }
}